package com.cheng.sell.controller;

import com.cheng.sell.VO.ResultVO;
import com.cheng.sell.converter.OrderForm2OrderMasterDTO;
import com.cheng.sell.dto.OrderMasterDTO;
import com.cheng.sell.enums.ResultEnum;
import com.cheng.sell.exception.SellException;
import com.cheng.sell.form.OrderForm;
import com.cheng.sell.service.BuyerService;
import com.cheng.sell.service.OrderMasterService;
import com.cheng.sell.utils.ResultVOUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description: 订单模块
 * @author:
 * @create: 2018-09-02
 */
@RestController
@RequestMapping("/buyer/order")
@Slf4j
public class OrderController {
    @Autowired
    private OrderMasterService orderMasterService;

    @Autowired
    private BuyerService buyerService;

    @PostMapping("/create")
    @ApiOperation(value="根据用户编号获取用户姓名", notes="test: 仅1和2有正确返回")
    public ResultVO<Map<String, String>> create(@Valid OrderForm orderForm, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            log.error("【创建订单】参数不正确 orderForm={}", orderForm);
            throw new SellException(ResultEnum.PARAM_ERROR.getCode(), bindingResult.getFieldError().getDefaultMessage());
        }

        OrderMasterDTO orderMasterDTO = OrderForm2OrderMasterDTO.converter(orderForm);
        if (CollectionUtils.isEmpty(orderMasterDTO.getOrderDetailList())) {
            log.error("【创建订单】购物车不能为空");
            throw new SellException(ResultEnum.CART_EMPTY);
        }
        OrderMasterDTO omd = orderMasterService.create(orderMasterDTO);
        Map<String, String> result = new HashMap<>();
        result.put("orderId", omd.getOrderId());
        return ResultVOUtil.success(result);
    }

    @GetMapping("/list")
    public ResultVO<List<OrderMasterDTO>> list(@RequestParam String openId,
                                               @RequestParam(value = "page", defaultValue = "0") Integer page,
                                               @RequestParam(value = "size", defaultValue = "10") Integer size) {
        if (StringUtils.isEmpty(openId)) {
            log.error("【订单列表查询】");
            throw new SellException(ResultEnum.PARAM_ERROR);
        }
        PageRequest pageRequest = new PageRequest(page, size);
        Page<OrderMasterDTO> orderMasterDTOPage = orderMasterService.findByBuyerOpenid(openId, pageRequest);
        return ResultVOUtil.success(orderMasterDTOPage.getContent());
    }

    @GetMapping("/query")
    public ResultVO<OrderMasterDTO> query(@RequestParam(value = "openId") String openId,
                                          @RequestParam(value = "orderId") String orderId) {

        if (StringUtils.isEmpty(openId)) {
            log.error("【订单查询】");
            throw new SellException(ResultEnum.PARAM_ERROR);
        }

        OrderMasterDTO orderMasterDTO = buyerService.findOrderOne(openId, orderId);
        return ResultVOUtil.success(orderMasterDTO);
    }

    @PostMapping("/cancel")
    public ResultVO cancel(@RequestParam(value = "openId") String openId,
                           @RequestParam(value = "orderId") String orderId) {
        buyerService.cancelOrder(openId, orderId);
        return ResultVOUtil.success();
    }
}
